<?php
include '../template.php'; 
include './blastparser.php';
include 'blast_fun.php';
include '../dbconfig/config.php';
include '../dbconfig/opendb.php';
include '../util/dbutil.php';
getTopWithJS("..");
?>


&nbsp;<br />
<?php
	if(!isset($_POST['blast'])) {
		die('Please input peptide sequence!');
	}        

        $blastres = $_POST['blastres'];
        $seq = $_POST['seq'];
        $seq = blastseqpack($seq);
        $org = $_POST['org'];
        $ref = $_POST['ref'];
        $ecut = $_POST['evalue'];

        $wd = "./";
        
        if (strlen($seq)<5) {
                die("Please input a query protein sequence with length &ge;5");
        }

        if (isset($blastres)) {
            $data = unserializeFromDb($blastres);
            if (count($data)==1)
                $hits = $data[0];
        }

        if (!isset($hits)) {
            $blastout = run_blast_nr_peptide($wd,$seq,$ecut,$org,$ref);
            $hits = blastparse($blastout);
            unlink($blastout);
        }
	
	$count = count($hits);
	if ($count==0) {
                errormsg($ecut);
	} else { //>0
                blastalign($seq,$hits);

                $evalues = getevalues($hits);

                $aaperline = 60;
                $seqarrs = blastformat($seq,$hits,$evalues,$aaperline,$org,$ref);
//                $nhit = $fmtret["nhit"];
//                if ($nhit==0) {
//                        errormsg($ecut);
//                } else {
                        //$seqarrs = $fmtret["seqarrs"];
                        //print_r($seqarrs);

                        $legend = color_legend($evalues);

                        echo "<table border=\"0\">";
                        echo "<tr><td colspan=\"2\" >";

                        echo "<table width=\"570\" border=\"0\"><tr valign=\"bottom\">";
                        echo "<td width=\"300\"><table class=\"legend\">";
                        echo "<tr><td><b>$count substrate peptide hits were found<br/> with E-value&le;$ecut</b></td>";
                        echo "</tr></table></td>";

                        echo "<td width=\"300\" align=\"right\"><table class=\"legend\">";
                        echo "<tr><td align=\"right\"><font size=\"1\"><u>BLAST E-value color scheme<u></font></td></tr>";
                        echo "<tr><td>$legend</td></tr>";
                        echo "</table></td>";

                        echo "</td></tr></table>";



                        echo "<tr><td colspan=\"2\">&nbsp;</td></tr>";

                        $t = 0;
                        foreach ($seqarrs as $seqarr) {
                                $s = $t+1;
                                echo "<tr><td><b>Query: $s</b> &nbsp;</td>";

                                $query = $seqarr["query"];//current($seqarr);
                                echo "<td><font face=\"Courier New, Courier, Monaco, ProFont, Monotype Corsiva, Base Monospace\">&nbsp;$query&nbsp;</font></td>\n";
                                $t = $s + $aaperline - substr_count($query,"-")-1;
                                //echo "<td>&nbsp;<b>$t</b></td></tr>";

                                //next($seqarr);

                                echo "<tr><td>&nbsp;</td><td>";
                                echo "<font face=\"Courier New, Courier, Monaco, ProFont, Monotype Corsiva, Base Monospace\">";
                                //while (current($seqarr)) {
                                if (count($seqarr["sbjct"])>0) {
                                        foreach ($seqarr["sbjct"] as $sbjct) {
                                                echo $sbjct."<br/>\n";
                                        }
                                }
                                echo "</font>\n";
                                //echo "</td><td>&nbsp</td></tr>\n";

                                echo "<tr><td colspan=\"2\">&nbsp;</td></tr>";
                                //echo "<tr><td colspan=\"3\"><hr/></td></tr>";


                        }

                        //$blastouturl = "./tmp/".basename($blastout);

                        echo "<tr><td colspan=\"2\" align=\"right\">";
                        echo "<table><tr>";
                        //echo "<td align=\"right\"><form method=\"post\" action=\"$blastouturl\"><input name=\"res\" type=\"submit\" id=\"res\" value=\"Get BLAST alignment file\"></form ></td>";
                        echo "<td><FORM><INPUT TYPE=\"button\" VALUE=\"Go back\" onClick=\"history.go(-1);return true;\"> </FORM></td>";
                        //echo "<td align=\"right\"><a href=\"$blastouturl\">Get BLAST alignment file</a></td>";
                        echo "<td align=\"right\"><form method=\"post\" action=\"./pepbrowser.php\"><input name=\"new\" type=\"submit\" id=\"new\" value=\"New inquiry\"></form ></td>";
                        echo "</tr></table>";
                        echo "</td></tr>";

                        echo "</table>";

//                }
                
	}
	include '../dbconfig/closedb.php';

        function errormsg($ecut) {
                echo "&nbsp;<br />";
		echo "Ooops... No significant hit was found (E-value&le;$ecut).<br/> ".
                     "Please increase the E-value threshold (i.e. less stringent) if possible, and try again.";
        }
?>
	

<?php getBottom(); ?>
